package com.lb.jwitter.action;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTChuyenNganh;
import com.lb.jwitter.service.TblCTDTChuyenNganhService;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVien;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVienService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblTaiKhoanService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Encrypt;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.lb.jwitter.service.TblThongTinHocVienMonHoc;
import com.lb.jwitter.service.TblThongTinHocVienMonHocService;

@ContextConfiguration(locations="classpath:beans.xml")
public class TaiKhoanHocVienAction
{
	
	private String username;
	private String oldPassword;
	private String newPassword;
	
	private String code;
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	private String msg;
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public TblDKMHTaiKhoanHocVienService getTblDKMHTaiKhoanHocVienService() {
		return tblDKMHTaiKhoanHocVienService;
	}

	public void setTblDKMHTaiKhoanHocVienService(
			TblDKMHTaiKhoanHocVienService tblDKMHTaiKhoanHocVienService) {
		this.tblDKMHTaiKhoanHocVienService = tblDKMHTaiKhoanHocVienService;
	}

	TblDKMHTaiKhoanHocVienService tblDKMHTaiKhoanHocVienService;
	
	 @Autowired // Injected by Spring when bean is "ready"
	 ApplicationContextProvider contextProvider;

public String execute() throws Exception
{
   	try{
   
       
       Map session = ActionContext.getContext().getSession();

       TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien =  (TblDKMHTaiKhoanHocVien) session.get("taiKhoanHocVien");
	     if( tblDKMHTaiKhoanHocVien == null) return "noprivilege";
	     
	    ApplicationContext context = ApplicationContextProvider.getApplicationContext();

	   tblDKMHTaiKhoanHocVienService = (TblDKMHTaiKhoanHocVienService) context.getBean("tbl_DKMH_TaiKhoanHocVienClient");
	   if(oldPassword != null && newPassword != null &&
			   tblDKMHTaiKhoanHocVien.getPassword().equals(Encrypt.sha(oldPassword, "SHA-1"))){
		   
		   if(oldPassword.equals(newPassword)){
		        return Action.SUCCESS;
		   }
		   
		   tblDKMHTaiKhoanHocVien.setPassword(Encrypt.sha(newPassword, "SHA-1"));
		   tblDKMHTaiKhoanHocVienService.updatetblDKMHTaiKhoanHocVien(tblDKMHTaiKhoanHocVien);
	   }else{
		   code = "ERROR";
		   //Password khong dung, vui long nhap lai
		   msg = "Password không đúng, vui lòng thử lại!!";
		   
		   return Action.ERROR;
	   }
       
        return Action.SUCCESS;

     }
        catch(Exception e){
            e.printStackTrace();
            code = "ERROR";
 		   msg = "Có lỗi xảy ra, vui lòng báo lại cho Nhân viên quản trị!!!";
 		   
            return Action.ERROR;
      }
       
}	
}
